import os

# --- 配置 (与下载脚本保持一致) ---
TIME_INTERVALS = [
    "12h", "15m", "1d", "1h", "1m", "1mo", "1w", "2h",
    "30m", "3d", "3m", "4h", "5m", "6h", "8h"
]
START_DATE_STR = "2020-01"
END_DATE_STR = "2025-04"  # 包含此月份
SYMBOL = "ETHUSDT"

# 请确保以下路径存在
EXTRACT_BASE_DIR = r"F:\personal\binance_klines"  # 解压文件的基础目录, 例如 F:\personal\binance_klines


# --- 主逻辑 ---
def main():
    print(f"开始验证缺失的CSV文件, 基础路径: {EXTRACT_BASE_DIR}")
    print(f"时间范围: {START_DATE_STR} 至 {END_DATE_STR}")
    print(f"交易对: {SYMBOL}")
    print("-" * 50)

    if not os.path.isdir(EXTRACT_BASE_DIR):
        print(f"错误: 基础解压目录 '{EXTRACT_BASE_DIR}' 不存在。请先运行下载和解压程序。")
        return

    start_year, start_month = map(int, START_DATE_STR.split('-'))
    end_year, end_month = map(int, END_DATE_STR.split('-'))

    total_missing_files_overall = 0

    for interval in TIME_INTERVALS:
        print(f"\n正在检查时间类型: {interval}")
        interval_extract_path = os.path.join(EXTRACT_BASE_DIR, interval)
        missing_files_for_interval = 0

        if not os.path.isdir(interval_extract_path):
            print(f"  警告: 目录 '{interval_extract_path}' 不存在。该时间类型下的所有文件都将被视为缺失。")
            # 计算理论上应该有多少文件 (用于计数)
            temp_year, temp_month = start_year, start_month
            expected_files_count = 0
            while temp_year < end_year or (temp_year == end_year and temp_month <= end_month):
                expected_files_count += 1
                temp_month += 1
                if temp_month > 12:
                    temp_month = 1
                    temp_year += 1
            print(f"  理论上应有 {expected_files_count} 个文件。")
            missing_files_for_interval = expected_files_count  # 所有文件都缺失
            total_missing_files_overall += missing_files_for_interval
            # 在这种情况下，下面的循环仍然会执行并报告每个文件缺失
            # 但我们可以选择在这里直接continue，如果目录不存在就没必要再检查具体文件了
            # continue # 如果希望跳过不存在目录的详细文件检查

        current_year = start_year
        current_month = start_month

        while current_year < end_year or (current_year == end_year and current_month <= end_month):
            year_str = str(current_year)
            month_str = f"{current_month:02d}"  # 月份补零

            # 预期的CSV文件名格式: SYMBOL-interval-YYYY-MM.csv
            expected_csv_filename = f"{SYMBOL}-{interval}-{year_str}-{month_str}.csv"
            expected_csv_path = os.path.join(interval_extract_path, expected_csv_filename)

            if not os.path.exists(expected_csv_path):
                print(f"  缺失: {expected_csv_filename} (路径: {expected_csv_path})")
                missing_files_for_interval += 1
                total_missing_files_overall += 1  # 累加到总数

            # 更新月份和年份
            current_month += 1
            if current_month > 12:
                current_month = 1
                current_year += 1
                if current_year > end_year:  # 确保不会超出结束年份
                    break

        if os.path.isdir(interval_extract_path):  # 只有当目录存在时才打印这个总结
            if missing_files_for_interval == 0:
                print(f"  在 {interval_extract_path} 中未发现缺失文件。")
            else:
                print(f"  在 {interval_extract_path} 中共发现 {missing_files_for_interval} 个缺失文件。")
        elif missing_files_for_interval > 0:  # 目录不存在，但我们已经报告了缺失
            print(f"  由于目录 {interval_extract_path} 不存在, {missing_files_for_interval} 个文件被视为缺失。")

    print("-" * 50)
    if total_missing_files_overall == 0:
        print("恭喜！所有预期时间范围内的CSV文件均已存在。")
    else:
        print(f"总共检测到 {total_missing_files_overall} 个缺失的CSV文件。")


if __name__ == "__main__":
    main()